package com.example.hanchen.yongkanggy.http;

import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.stream.Collectors;

import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class FileUploadTask implements Runnable {
    private String filePath;
    private String uploadUrl;

    public FileUploadTask(String filePath, String uploadUrl) {
        this.filePath = filePath;
        this.uploadUrl = uploadUrl;
    }

    @Override
    public void run() {
        File file = new File(filePath);
        try {
            URL url = new URL(uploadUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setUseCaches(false);
            connection.setDoOutput(true); // 设置为输出模式
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("ENCTYPE", "multipart/form-data");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + "*****");
//            connection.setRequestProperty("file", file.getName());
            connection.setRequestProperty("moduleName", "Question Feedback");

            DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
            dos.writeBytes("--" + "*****" + "\r\n");
            dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"" + "\r\n");
            dos.writeBytes("Content-Type: " + "application/octet-stream" + "\r\n");
            dos.writeBytes("\r\n");

            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[8192]; // 8k
            int count;
            while ((count = fis.read(buffer)) != -1) {
                dos.write(buffer, 0, count);
            }
            fis.close();

            dos.writeBytes("\r\n--" + "*****" + "--\r\n");
            dos.flush();
            dos.close();

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 上传成功

                String serverResponse = new BufferedReader(new InputStreamReader(connection.getInputStream())).lines().collect(Collectors.joining("\n"));

                Log.i("JFOEIFJEIFDDDDD", "成功"+serverResponse);
            } else {
                // 上传失败
                Log.i("JFOEIFJEIFDDDDD", "失败");
            }

            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static void uploadImage(File imageFile, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", imageFile.getName(), RequestBody.create(MediaType.parse("image/*"), imageFile))
                .addFormDataPart("moduleName", "Question Feedback")
                .build();

        Request request = new Request.Builder()
                .url("http://192.168.0.2:8009/verification/img")
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);
    }
}